package com.samsungmobile.common.util;

import java.util.Enumeration;
import java.util.Vector;

public class StringArray
{

    public StringArray(StringArray stringarray)
    {
        this(stringarray.toArray());
    }

    public StringArray(String as[])
    {
        array = null;
        size = 0;
        array = as;
        size = as.length;
    }

    public StringArray()
    {
        array = null;
        size = 0;
        array = new String[initSize];
    }

    public StringArray(int i)
    {
        array = null;
        size = 0;
        array = new String[i];
    }

    public StringArray(String s)
    {
        array = null;
        size = 0;
        array = new String[initSize];
        array[0] = s;
        size = 1;
    }

    public StringArray(String s, String s1)
    {
        array = null;
        size = 0;
        if(s == null || s.length() < 1)
        {
            array = new String[initSize];
        } else
        {
            array = split(s, s1);
            size = array.length;
        }
    }

    public int setSize(int i)
    {
        if(size < i)
        {
            int j = i - size;
            increasArray(j);
            size += j;
        }
        return size;
    }

    public StringArray append(String s)
    {
        addElement(s);
        return this;
    }

    public StringArray append(String as[])
    {
        addElement(as);
        return this;
    }

    public StringArray apppend(Enumeration enumeration)
    {
        if(enumeration == null)
            return this;
        for(; enumeration.hasMoreElements(); append((String)enumeration.nextElement()));
        return this;
    }

    public StringArray append(StringArray stringarray)
    {
        return append(stringarray.toArray());
    }

    public StringArray removeNotFound(StringArray stringarray)
    {
        String as[] = toArray();
        for(int i = 0; i < as.length; i++)
            if(!stringarray.contains(as[i]))
                remove(as[i]);

        return this;
    }

    private void increasArray(int i)
    {
        if(size + i > array.length)
        {
            int j = ((size + i) / extendSize + 1) * extendSize;
            if(j < initSize)
                j = initSize;
            String as[] = new String[j];
            System.arraycopy(array, 0, as, 0, array.length);
            array = as;
        }
    }

    public void addElement(String s)
    {
        if(size >= array.length)
            increasArray(1);
        array[size++] = s;
    }

    public void addElement(String as[])
    {
        if(as == null)
            return;
        if(size + as.length > array.length)
            increasArray(as.length);
        System.arraycopy(as, 0, array, size, as.length);
        size += as.length;
    }

    public String set(int i, String s)
    {
        if(i < 0 || i >= size)
            throw new ArrayIndexOutOfBoundsException(i + " (current max index = " + (size - 1) + ")");
        else
            return array[i] = s;
    }

    public String[] toArray()
    {
        if(size <= 0)
        {
            return null;
        } else
        {
            String as[] = new String[size];
            System.arraycopy(array, 0, as, 0, size);
            return as;
        }
    }

    public String toString()
    {
        return join();
    }

    public boolean isEmpty()
    {
        return size == 0;
    }

    public void clear()
    {
        size = 0;
        array = null;
    }

    public int remove(String s)
    {
        int i = indexOf(s);
        if(i != -1)
            remove(i);
        return i;
    }

    public int remove(StringArray stringarray)
    {
        return remove(stringarray.toArray());
    }

    public int remove(String as[])
    {
        int i = 0;
        for(int j = 0; j < as.length; j++)
            if(remove(as[j]) != -1)
                i++;

        return i;
    }

    public String remove(int i)
    {
        Object obj = null;
        if(i < 0 || i >= size)
        {
            throw new ArrayIndexOutOfBoundsException(i + " (current max index = " + (size - 1) + ")");
        } else
        {
            String s = array[i];
            System.arraycopy(array, i + 1, array, i, size - i - 1);
            size--;
            return s;
        }
    }

    public int length()
    {
        return size;
    }

    public String elementAt(int i)
    {
        if(i < 0 || i >= size)
            throw new ArrayIndexOutOfBoundsException(i + " (current max index = " + (size - 1) + ")");
        else
            return array[i];
    }

    public boolean contains(String s)
    {
        return indexOf(s) != -1;
    }

    public int indexOf(String s)
    {
        return indexOf(array, s, 0, size);
    }

    public int indexOf(String s, int i)
    {
        return indexOf(array, s, i, array.length);
    }

    public static int indexOf(String as[], String s)
    {
        return indexOf(as, s, 0, as.length);
    }

    public static int indexOf(String as[], String s, int i)
    {
        return indexOf(as, s, i, as.length);
    }

    public static int indexOf(String as[], String s, int i, int j)
    {
        for(int k = i; k < j; k++)
            if(as[k].equals(s))
                return k;

        return -1;
    }

    public int lastIndexOf(String s)
    {
        return lastIndexOf(array, s, size - 1);
    }

    public int lastIndexOf(String s, int i)
    {
        return lastIndexOf(array, s, i);
    }

    public static int lastIndexOf(String as[], String s)
    {
        return lastIndexOf(as, s, as.length - 1);
    }

    public static int lastIndexOf(String as[], String s, int i)
    {
        for(int j = i; j >= 0; j--)
            if(as[j].equals(s))
                return j;

        return -1;
    }

    public static String[] split(String s, String s1, int i, int j)
    {
        int k = 0;
        int l = 0;
        int i1 = s.length();
        StringArray stringarray = new StringArray(i);
        extendSize = j;
        do
        {
            int j1 = s.indexOf(s1, k);
            if(j1 == -1)
            {
                if(k < i1)
                {
                    stringarray.addElement(s.substring(k).trim());
                    l++;
                }
                break;
            }
            if(k < i1)
            {
                stringarray.addElement(s.substring(k, j1).trim());
                l++;
            }
            k = j1 + s1.length();
        } while(true);
        return l != 0 ? stringarray.toArray() : null;
    }

    public static String[] split(String s, String s1)
    {
        return split(s, s1, initSize, extendSize);
    }

    public static String[] split(String s, String s1, int i)
    {
        return split(s, s1, i, extendSize);
    }

    public static String[] split2(String s, String s1)
    {
        Vector vector = new Vector();
        int i = 0;
        do
        {
            int j = s.indexOf(s1, i);
            if(j == -1)
            {
                vector.addElement(s.substring(i));
                break;
            }
            vector.addElement(s.substring(i, j));
            i = j + s1.length();
        } while(true);
        if(vector.size() < 1)
            return null;
        String as[] = new String[vector.size()];
        for(int k = 0; k < vector.size(); k++)
            as[k] = (String)vector.elementAt(k);

        return as;
    }

    public String join()
    {
        return join(array, 0, size, ",");
    }

    public String join(String s)
    {
        return join(array, 0, size, s);
    }

    public static String join(String as[])
    {
        return join(as, ",");
    }

    public static String join(String as[], String s)
    {
        if(as == null)
            return null;
        else
            return join(as, 0, as.length, s);
    }

    public static String join(int ai[], String s)
    {
        if(ai == null)
            return null;
        else
            return join(ai, 0, ai.length, s);
    }

    public static String join(String as[], int i, int j, String s)
    {
        if(as == null || j < 1)
            return null;
        StringBuffer stringbuffer = new StringBuffer();
        stringbuffer.append(as[i++]);
        try
        {
            while(--j > 0) 
                stringbuffer.append(s + as[i++]);
        }
        catch(ArrayIndexOutOfBoundsException arrayindexoutofboundsexception) { }
        return stringbuffer.toString();
    }

    public static String join(int ai[], int i, int j, String s)
    {
        if(ai == null || j < 1)
            return null;
        StringBuffer stringbuffer = new StringBuffer();
        stringbuffer.append(ai[i++]);
        try
        {
            while(--j > 0) 
                stringbuffer.append(s + ai[i++]);
        }
        catch(ArrayIndexOutOfBoundsException arrayindexoutofboundsexception) { }
        return stringbuffer.toString();
    }

    public static String fill(String s, int i)
    {
        return fill(s, i, ",");
    }

    public static String fill(String s, int i, String s1)
    {
        StringBuffer stringbuffer = new StringBuffer();
        stringbuffer.append(s);
        for(int j = 1; j < i; j++)
            stringbuffer.append(s1 + s);

        return stringbuffer.toString();
    }

    private String array[];
    private static int initSize = 10;
    private static int extendSize = 10;
    private int size;
    private static final String defaultDelimiter = ",";

}
